from fogmini import global_objects

people = []

def get_name_for_email(email):
    load_people()

    for person in people:
        if person.email == email:
            return person.name

    raise IndexError, "No person found with email address %s." % (email, )

def load_people():
    global people

    if len(people) > 0:
        return

    people = [Person(xml) for xml in global_objects.fb.listPeople().findAll("person")]

class Person(object):
    def __init__(self, xml):
        self.name = xml.find("sfullname").contents[0]
        self.email = xml.find("semail").contents[0]

    def __str__(self):
        return "%s (%s)" % (self.name, self.email)
